import http from "@/api";

// 工位相关接口
export namespace Workstation {
  // 创建工位请求参数
  export interface CreateWorkstationRequestDto {
    workstationCode: string;
    workstationName: string;
    productionLineId: string; // 改为string类型避免精度丢失
    workstationType: string;
    status?: string;
    capacity?: number;
    position?: number;
    supportedProcessTypes?: string;
    description?: string;
  }

  // 更新工位请求参数
  export interface UpdateWorkstationRequestDto {
    workstationName: string;
    productionLineId: string; // 改为string类型避免精度丢失
    workstationType: string;
    status?: string;
    capacity?: number;
    position?: number;
    supportedProcessTypes?: string;
    description?: string;
  }

  // 工位列表项
  export interface WorkstationListItem {
    id: string;
    workstationCode: string;
    workstationName: string;
    productionLineId: string; // 改为string类型避免精度丢失
    productionLineName: string;
    workshopId: string; // 改为string类型避免精度丢失
    workshopName: string;
    factoryId: string; // 改为string类型避免精度丢失
    factoryName: string;
    workstationType: string;
    status: string;
    capacity?: number;
    position?: number;
    location: string;
    supportedProcessTypes?: string;
    description?: string;
    createTime: string;
    updateTime: string;
  }

  // 工位列表响应
  export interface WorkstationListResponse {
    records: WorkstationListItem[];
    total: number;
  }
}

/** 创建工位 */
export const createWorkstation = (params: Workstation.CreateWorkstationRequestDto) => {
  return http.post<void>("/workstation/create", params);
};

/** 获取工位列表 */
export const getWorkstationList = (params?: {
  workstationCode?: string;
  workstationName?: string;
  productionLineId?: string; // 改为string类型避免精度丢失
  workshopId?: string; // 改为string类型避免精度丢失
  factoryId?: string; // 改为string类型避免精度丢失
  workstationType?: string;
  status?: string;
  current?: number;
  size?: number;
}) => {
  return http.get<Workstation.WorkstationListResponse>("/workstation/page", params);
};

/** 更新工位 */
export const updateWorkstation = (id: string, params: Workstation.UpdateWorkstationRequestDto) => {
  return http.put<void>(`/workstation/${id}`, params);
};

/** 删除工位 */
export const deleteWorkstation = (id: string) => {
  return http.delete<void>(`/workstation/${id}`);
};

/** 批量删除工位 */
export const batchDeleteWorkstation = (ids: string[]) => {
  return http.delete<void>("/workstation/batch", { data: { ids } });
};

/** 获取工位详情 */
export const getWorkstationById = (id: string) => {
  return http.get<Workstation.WorkstationListItem>(`/workstation/${id}`);
};

/** 根据产线ID获取工位列表 */
export const getWorkstationsByProductionLineId = (productionLineId: string) => {
  return http.get<Workstation.WorkstationListItem[]>(`/workstation/productionLine/${productionLineId}`);
};

/** 根据产线ID获取启用状态的工位列表 */
export const getEnabledWorkstationsByProductionLineId = (productionLineId: string) => {
  return http.get<Workstation.WorkstationListItem[]>(`/workstation/productionLine/${productionLineId}/enabled`);
};

/** 获取所有启用状态的工位列表 */
export const getAllEnabledWorkstations = () => {
  return http.get<Workstation.WorkstationListItem[]>("/workstation/enabled");
};

/** 获取可进站的工位列表 */
export const getAvailableWorkstationsForIn = () => {
  return http.get<Workstation.WorkstationListItem[]>("/workstation/availableForIn");
};

/** 获取可出站的工位列表 */
export const getAvailableWorkstationsForOut = () => {
  return http.get<Workstation.WorkstationListItem[]>("/workstation/availableForOut");
};

/** 获取启用状态的产线列表 */
export const getEnabledProductionLineList = () => {
  return http.get<{ id: string; lineName: string; factoryName: string; workshopName: string }[]>("/productionLine/enabled");
};


export namespace WorkstationOperation {
  export interface OperationRequest {
    workstationId: number;
    workOrderCode: string;
    serialNumber: string;
    operationType?: string;
    operatorCode?: string;
    operationTime?: string;
    remark?: string;
  }

  export interface OperationRecord {
    id: number;
    workstationId: number;
    workstationName: string;
    workOrderCode: string;
    serialNumber: string;
    productName: string;
    operationType: string;
    operatorCode: string;
    operatorName: string;
    operationTime: string;
    processingDuration: number;
    status: string;
    remark: string;
  }

  export interface RuntimeStatus {
    workstationId: number;
    workstationName: string;
    workstationCode: string;
    currentOperationId?: number;
    currentWorkOrderCode?: string;
    serialNumber?: string;
    currentInTime?: string;
    currentOutTime?: string;
    status: string;
    operatorName?: string;
    runningDuration?: number;
  }

  export interface ReportData {
    workstationId: number;
    workstationName: string;
    reportDate: string;
    totalInCount: number;
    totalOutCount: number;
    qualifiedCount: number;
    unqualifiedCount: number;
    qualificationRate: number;
    avgProcessingDuration: number;
    maxProcessingDuration: number;
    minProcessingDuration: number;
    utilizationRate: number;
    hourlyProduction: HourlyProduction[];
  }

  export interface HourlyProduction {
    hour: number;
    inCount: number;
    outCount: number;
  }
}


/**
 * @name 工位操作模块
 */
// 工位进站
export const stationIn = (params: WorkstationOperation.OperationRequest) => {
  return http.post<number>("/workstation/station-in", params);
};

// 工位出站
export const stationOut = (params: WorkstationOperation.OperationRequest) => {
  return http.post<number>("/workstation/station-out", params);
};

// 获取操作记录
export const getOperationRecords = (params: any) => {
  return http.get<WorkstationOperation.OperationRecord[]>("/workstation/operation-records", params);
};

// 获取工位报表
export const getWorkstationReport = (params: any) => {
  return http.get<WorkstationOperation.ReportData[]>("/workstation/report", params);
};

// 获取实时状态
export const getRealTimeStatus = () => {
  return http.get<WorkstationOperation.RuntimeStatus[]>("/workstation/real-time-status");
};

// 工单选项接口
export interface WorkOrderOption {
  id: number;
  workOrderCode: string;
  productId: number;
  productName: string;
}

// 序列号选项接口
export interface SerialNumberOption {
  serialNumber: string;
  productName: string;
}

// 获取可用工单号列表
export const getAvailableWorkOrders = () => {
  return http.get<WorkOrderOption[]>("/workorders/availableWorkOrders");
};

// 获取可用产品序列号列表
export const getAvailableSerialNumbers = (workOrderCode?: string) => {
  return http.get<SerialNumberOption[]>("/products/availableSerialNumbers", {
    workOrderCode
  });
};